/*
 * Proto Streamer
 * Copyright (C) 2009 John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package p.c.http;

import p.c.Date;
import p.c.Headers;
import p.c.Input;
import p.c.Output;

import java.net.URL;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

/**
 * @author jdp
 */
public final class Request 
    extends Object
{
    protected final byte[] Ascii(java.lang.String string){
        int len = string.length();
        byte[] bytes = new byte[len];
        string.getBytes(0,len,bytes,0);
        return bytes;
    }
    public static class Output
        extends p.c.Output
    {
        private Connection connection;

        Output(Connection connection) throws IOException {
            super(connection.openOutputStream());
            this.connection = connection;
        }

        public void close() throws IOException {
            try {
                super.close();
            }
            finally {
                this.connection.disconnect();
            }
        }
    }


    private Connection connection;
    private Output out;
    private Headers headers = new Headers();
    private java.lang.String requestline;


    public Request(Connection connection)
        throws IOException
    {
        super();
        this.connection = connection;
        String hostport = connection.getHostPort();
        if (null != hostport){
            this.headers.set("Host",hostport);
        }
    }


    public OutputStream getOutputStream() 
        throws IOException 
    {
        Output out = this.out;
        if (null == out){
            Connection connection = this.connection;
            this.out = new Request.Output(connection);
            byte[] requestline = Ascii(this.requestline = connection.getRequestLine());
            this.out.write(requestline);
            this.headers.write(this.out);
        }
        return this.out;
    }
    public void setHeader(String name, String value){
        this.headers.set(name,value);
    }
    public final boolean hasContentLength(){
        return this.headers.contains("Content-Length");
    }
    public final boolean hasNotContentLength(){
        return this.headers.containsNot("Content-Length");
    }
    public final void setContentLength(int length){
        this.headers.set("Content-Length",String.valueOf(length));
    }
    public final void setContentEncoding(String value){
        this.headers.set("Content-Encoding",value);
    }
    public final void setLocation(java.lang.String value){
        this.headers.set("Location",value);
    }
    public final void setContentType(java.lang.String value){
        this.headers.set("Content-Type",value);
    }
    public final void setContentTypeText(){
        this.setContentType("text/plain");
    }
    public final void setContentTypeHtml(){
        this.setContentType("text/html");
    }
    public final void setContentTypeXml(){
        this.setContentType("text/xml");
    }
    public final void setContentTypeJson(){
        this.setContentType("application/json");
    }
    public final void setLastModified(long time){
        this.headers.setDate("Last-Modified",java.lang.System.currentTimeMillis());
    }
    public final String getConnection(){
        return this.headers.valueOf("Connection");
    }
    public final void setConnectionClose(){
        this.headers.set("Connection","close");
    }
    public final void setDate(){
        this.headers.setDate("Date",java.lang.System.currentTimeMillis());
    }
    public final void setApplication(String version){
        this.headers.set("Application",version);
    }
    public final void setFormatDate(java.lang.String name, long time){
        this.headers.setDate(name,time);
    }
}
